package com.ruoyi.common.enums.wecom;

import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Getter
@AllArgsConstructor
public enum WecomMsgTypeEnum {

    TEXT("text", "文本消息"),
    IMAGE("image", "图片消息"),
    REVOKE("revoke", "撤回消息"),
    DISAGREE("disagree", "会话存档不同意消息"),
    AGREE("agree", "会话存档同意消息"),
    VOICE("voice", "语音消息"),
    VIDEO("video", "视频消息"),
    CARD("card", "名片消息"),
    LOCATION("location", "位置消息"),
    EMOTION("emotion", "表情消息"),
    FILE("file", "文件消息"),
    LINK("link", "链接消息"),
    WEAPP("weapp", "小程序消息"),
    CHAT_RECORD("chatrecord", "会话记录消息"),
    TODO("todo", "待办消息"),
    VOTE("vote", "投票消息"),
    COLLECT("collect", "填表消息"),
    RED_PACKET("redpacket", "红包消息"),
    MEETING("meeting", "会议邀请消息"),
    MEETING_NOTIFICATION("meeting_notification", "会议控制消息"),
    DOCMSG("docmsg", "在线文档消息"),
    MARKDOWN("markdown", "MarkDown格式消息"),
    NEWS("news", "图文消息"),
    CALENDAR("calendar", "日程消息"),
    MIXED("mixed", "日程消息"),
    MEETING_VOICE_CALL("meeting_voice_call", "音频存档消息"),
    EXTERNAL_RED_PACKET("external_redpacket", "互通红包消息"),
    SPH_FEED("sphfeed", "视频号消息"),
    VOIP_TEXT("voiptext", "音视频通话"),
    QY_DISK_FILE("qydiskfile", "微盘文件"),
    ;

    private final String code;
    private final String desc;

    private static final Map<String, WecomMsgTypeEnum> ENUM_MAP;

    static {
        WecomMsgTypeEnum[] values = values();
        Map<String, WecomMsgTypeEnum> codeToEnum = new HashMap<>(values.length);
        for (WecomMsgTypeEnum value : values) {
            codeToEnum.put(value.getCode(), value);
        }
        ENUM_MAP = Collections.unmodifiableMap(codeToEnum);
    }

    /**
     * 根据消息类型代码查找枚举
     */
    public static WecomMsgTypeEnum getEnumByCode(String code) {
        WecomMsgTypeEnum wecomMsgTypeEnum = ENUM_MAP.get(code);
        if (ObjectUtil.isEmpty(wecomMsgTypeEnum)) {
            throw new RuntimeException("消息类型有误: " + code);
        }
        return wecomMsgTypeEnum;
    }

    /**
     * 检查消息类型代码是否存在
     */
    public static boolean contains(String code) {
        return ENUM_MAP.containsKey(code);
    }

    /**
     * 安全获取枚举，如果不存在返回null
     */
    public static WecomMsgTypeEnum getEnumByCodeSafe(String code) {
        return ENUM_MAP.get(code);
    }
}
